package com.chrray.position.util.result;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.Page;

import java.util.List;

/**
 * @anthor ch
 * @description 返回数据封装
 * @date
 */
public class ResponseResults<T> {

    /************************基础封装*****************************/
    /**
     * 适用于增删改的情况
     */
    private static <T> ResponseResult<T> buildResponseResult(Integer code, String msg) {
        return new ResponseResult<>(code, msg);
    }

    /**
     * 适用于简单查询
     */
    private static <T> ResponseResult<T> buildResponseResult(Integer code, String msg, T data) {
        return new ResponseResult<>(code, msg, data);
    }

    /**
     * 适用于分页查询
     */
/*    private static <T> ResponseResult<T> buildResponseResult(Integer code, String msg, T data, Long total) {
        return new ResponseResult<T>(code, msg, data, total);
    }*/


    /************************成功封装*****************************/
    /**
     * 成功，简单增删改的响应结果
     */
    public static <T> ResponseResult<T> success() {
        return buildResponseResult(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
    }

    /**
     * 成功，简单增删改、修改提示信息
     *
     * @param msg 信息
     * @return 响应结果
     */
    public static <T> ResponseResult<T> success(String msg) {
        return buildResponseResult(ResultEnum.SUCCESS.getCode(), msg);
    }

    /**
     * 成功，简单增删改、自定义状态码
     *
     * @return 响应结果
     */
    public static <T> ResponseResult<T> success(ResultEnum resultEnum) {
        return buildResponseResult(resultEnum.getCode(), resultEnum.getMsg());
    }

    /**
     * 成功，简单增删改、自定义状态码、修改提示信息
     *
     * @param msg 信息
     * @return 响应结果
     */
    public static <T> ResponseResult<T> success(ResultEnum resultEnum, String msg) {
        return buildResponseResult(resultEnum.getCode(), msg);
    }


    /**
     * 适用于分页查询
     **/
/*    public static <T> ResponseResult<T> pageSuccess(Page<T> list) {
        return (ResponseResult<T>) buildResponseResult(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), list, list.getTotal());
    }*/
    /**
     * 适用于分页查询,自定义提示
     **/
/*
    public static <T> ResponseResult<T> pageSuccess(Page<T> list, String msg) {
        return (ResponseResult<T>) buildResponseResult(ResultEnum.SUCCESS.getCode(), msg, list, list.getTotal());
    }
*/


    /**
     * 成功，简单查询
     *
     * @return 响应结果
     */
    public static <T> ResponseResult<T> success(T data) {
        return buildResponseResult(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), data);
    }

    public static <T> ResponseResult<T> successData(T data) {
        return buildResponseResult(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), data);
    }

    /**
     * 成功，简单查询、修改提示信息
     *
     * @param msg 信息
     * @return 响应结果
     */
    public static <T> ResponseResult<T> success(T data, String msg) {
        return buildResponseResult(ResultEnum.SUCCESS.getCode(), msg, data);
    }


    /************************失败封装*****************************/


    /**
     * 失败，简单增删改的响应结果
     */
    public static <T> ResponseResult<T> fail() {
        return buildResponseResult(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
    }

    /**
     * 失败，简单增删改、修改提示信息
     *
     * @param msg 信息
     * @return 响应结果
     */
    public static <T> ResponseResult<T> fail(String msg) {
        return buildResponseResult(ResultEnum.FAIL.getCode(), msg);
    }

    /**
     * 失败，简单增删改、自定义状态码
     *
     * @return 响应结果
     */
    public static <T> ResponseResult<T> fail(ResultEnum resultEnum) {
        return buildResponseResult(resultEnum.getCode(), resultEnum.getMsg());
    }

    /**
     * 失败，简单增删改、自定义状态码、修改提示信息
     *
     * @param msg 信息
     * @return 响应结果
     */
    public static <T> ResponseResult<T> fail(ResultEnum resultEnum, String msg) {
        return buildResponseResult(resultEnum.getCode(), msg);
    }


    /**
     * 失败 适用于分页查询
     **/
/*
    public static <T> ResponseResult<T> fail(Page<T> list) {
        return (ResponseResult<T>) buildResponseResult(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg(), list, list.getTotal());
    }
*/


    /**
     * 失败，简单查询
     *
     * @return 响应结果
     */
    public static <T> ResponseResult<T> fail(T data) {
        return buildResponseResult(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg(), data);
    }

    /**
     * 失败，简单查询、修改提示信息
     *
     * @param msg 信息
     * @return 响应结果
     */
    public static <T> ResponseResult<T> fail(T data, String msg) {
        return buildResponseResult(ResultEnum.FAIL.getCode(), msg, data);
    }
    public static <T> ResponseResult<PageBean<T>> success(Page<T> page) {
        int code = ResultEnum.SUCCESS.getCode();
        String msg = ResultEnum.SUCCESS.getMsg();
        PageBean<T> data = buildPageBean(page.getTotal(), page.getResult());
        return buildResponseResult(code, msg, data);
    }
    private static <T> PageBean<T> buildPageBean(long total, List<T> rows) {
        return new PageBean<>(total, rows);
    }


    public static <T> ResponseResult<PageBean<T>> PAGE(IPage<T> page) {
        int code = ResultEnum.SUCCESS.getCode();
        String msg = ResultEnum.SUCCESS.getMsg();
        PageBean<T> data = buildPageBean(page.getTotal(), page.getRecords());
        return buildResponseResult(code, msg, data);
    }
}
